import React, { useState } from 'react';
interface Props {}
const getRouteParams = () => {
  const p = window.location.hash.split('#')[1];
  return p;
};
const PageB: React.FC<Props> = (props) => {
  const {} = props;
  const channel = new BroadcastChannel('text');
  const [message, setMessage] = useState('');

  document.title = getRouteParams();
  channel.onmessage = (e) => {
    // 接收消息
    console.log('收到消息：', e.data);
    setMessage(e.data);
    history.replaceState(null, '', `/pageB/#${e.data}`);
  };
  window.addEventListener('unload', () => {
    channel.postMessage({ cname: 'text', data: 'close' });
    channel.close();
  });

  return (
    <div>
      这是页面B------
      <span style={{ color: 'red' }}>{message || getRouteParams()}</span>
    </div>
  );
};
export default PageB;
